/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.dialog;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * Component used to enable dialog design<br>
 * Jquery use for this behavior is <code>$('#id').dialog(options);</code><br>
 * With Wicket, you just need to create a JQDialog Component
 * 
 * @author Benoit Bouchez
 * 
 */
public class JQDialog extends WebMarkupContainer implements IJQPluginComponent
{
	/** JQuery plugin */
	private JQDialogPlugin plugin;
	/** JQuery default options */
	private JQDialogOptions options = new JQDialogOptions();

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 */
	public JQDialog(String id, String key)
	{
		super(id);
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQDialogPlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQDialogPlugin(key);
		}
		this.plugin = (JQDialogPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(plugin.getKey()).attachComponentForDocumentReady(this);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 */
	public JQDialog(String id)
	{
		this(id, null);
	}

	public JQDialogOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQDialogOptions.class))
		{
			this.options = (JQDialogOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQDialog.class, options.getClass(), JQDialogOptions.class);
		}
	}

	public JQDialogPlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQDialogPlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + this.getMarkupId() + "')." + (reset ? "dialog('destroy')." : "") + options.getDocumentReadyOptions();
	}

	public String getAfterAjaxUpdateScript()
	{
		return "$('#" + this.getMarkupId() + "')." + options.getDocumentReadyOptions();
	}

	/**
	 * Open the dialog<br>
	 * Appends a javascript call <code>$('#id').dialog('open')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void open(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').dialog('open')");
	}

	// dialog( "isOpen" ) Returns: Boolean
	// Return if the dialog is currently open.

	/**
	 * Move the dialog to top<br>
	 * Appends a javascript call <code>$('#id').dialog('moveToTop')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void moveToTop(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').dialog('moveToTop')");
	}

	/**
	 * Close the dialog<br>
	 * Appends a javascript call <code>$('#id').dialog('close')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void close(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').dialog('close')");
	}

	/**
	 * Destroy the dialog<br>
	 * Appends a javascript call <code>$('#id').dialog('destroy')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void destroy(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').dialog('destroy')");
	}
}
